<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="90px">
      <el-form-item label="名称" prop="cName">
        <el-input
          v-model="queryParams.cName"
          placeholder="请输入城市合伙人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:company:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:company:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-on"
          size="mini"
          :disabled="single"
          @click="rechargeFn"
          v-hasPermi="['system:company:account']"
        >充值</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="城市合伙人id" align="center" prop="cId"  v-if="show" />
      <el-table-column label="城市合伙人名称" align="center" prop="cName" />
      <!-- <el-table-column label="显示顺序" align="center" prop="orderNum" /> -->
      <el-table-column label="所属区域" align="center" prop="areaCode">
          <template slot-scope="scope">
              {{scope.row.provinceName}} -
              {{scope.row.cityName}}
          </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="积分值" align="center" prop="accumulatePoints" />
      <el-table-column label="管理员账号" align="center" prop="remark" />
      <el-table-column label="城市合伙人状态" align="center" prop="status">
          <template slot-scope="scope">
              {{scope.row.status === 0 ? "正常": "停用"}}
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:company:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:company:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" :before-close="cancel" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="城市合伙人名称" prop="cName">
          <el-input v-model="form.cName" placeholder="请输入城市合伙人名称" />
        </el-form-item>
        <el-form-item label="所属省份" prop="provinceCode" >
            <el-select v-model="form.provinceObj" @change="getAreaListFn(2,form.provinceObj.id,'')" :disabled="isEdit"  placeholder="请选择所属省份" value-key="id" style="width: 100%">
                <el-option
                v-for="item in provinceList"
                :key="item.id"
                :label="item.area_name"
                :value="item">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="所属城市" prop="cityCode">
            <el-select v-model="form.cityObj" :disabled="isEdit" placeholder="请选择所属城市" value-key="id" style="width: 100%">
                <el-option
                v-for="item in cityList"
                :key="item.id"
                :label="item.area_name"
                :value="item">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="状态">
          <!-- <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group> -->
          <el-select v-model="form.status">
              <el-option label="正常" value="0" />
              <el-option label="停用" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="积分充值" :visible.sync="isShowRecharge" width="500px" append-to-body>
        <div class="dialog-container">
            <el-input v-model="accumulatePoint" placeholder="充值金额" />
        </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addAccumulateFn">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo, exportCityList,addCompanyAccumulate } from "@/api/system/company";

export default {
  name: "Company",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 公司信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      isEdit: false,
      // 是否显示弹出层
      open: false,
      //是否显示
      show: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cName: undefined,
        orderNum: undefined,
        areaCode: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: undefined,
      },
      // 表单参数
      form: {
        provinceObj: undefined,
        cityObj: undefined,
      },
        // 表单校验
        rules: {},
        provinceList: [],
        cityList: [],
        isShowRecharge: false,
        accumulatePoint: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
            this.infoList = response.rows;
            this.total = response.total;
            this.loading = false;
      });
    },
    getAreaListFn(type,id,isEdit){
        if(isEdit !== 'edit'){
            this.form.cityObj = undefined;
        }
        exportCityList({leveType: type,parentId: id}).then(res => {
            if(res.code === 'success'){
                for(let i = 0; i< res.data.length; i++){
                    delete res.data[i].parent_id;
                }
                if(type === 1){
                    this.provinceList = res.data;
                }else if(type === 2){
                    this.cityList = res.data;
                }
            }
        });
    },
    // 取消按钮
    cancel() {
        this.isEdit = false;
        this.open = false;
        this.reset();
    },
    // 表单重置
    reset() {
        this.form = {
            cId: undefined,
            cName: undefined,
            orderNum: undefined,
            areaCode: undefined,
            leader: undefined,
            phone: undefined,
            email: undefined,
            status: "0",
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            provinceObj: undefined,
            cityObj: undefined,
        };
        this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加城市合伙人信息";
      this.getAreaListFn(1,'','')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cId = row.cId || this.ids;
      this.getAreaListFn(1,'','');
      this.getAreaListFn(2,row.provinceCode,'');
      getInfo(cId).then(response => {
        this.form = JSON.parse(JSON.stringify(response.data));
        this.form.provinceObj = {
            id: this.form.provinceCode,
            area_name: this.form.provinceName,
        }
        let citys = {
            id: this.form.cityCode,
            area_name: this.form.cityName,
        };
        this.$set(this.form, 'cityObj', citys)
        this.open = true;
        this.title = "修改城市合伙人信息";
        this.isEdit = true;
      });

      this.form.provinceObj.attr-value(":disabled");

    },

    //充值操作
    rechargeFn(row) {
      this.reset();
      this.isShowRecharge = !this.isShowRecharge;
    },

    //关闭弹框
    close(){
        this.isShowRecharge = !this.isShowRecharge;
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            this.form.provinceCode = this.form.provinceObj.id;
            this.form.provinceName = this.form.provinceObj.area_name;
            this.form.cityCode = this.form.cityObj.id;
            this.form.cityName = this.form.cityObj.area_name;
            if (this.form.cId != undefined) {
                updateInfo(this.form).then(response => {
                if (response.code === 200) {
                    this.msgSuccess("修改成功");
                    this.cancel();
                    this.getList();
                }
                });
            } else {
                addInfo(this.form).then(response => {
                if (response.code === 200) {
                    this.msgSuccess("新增成功");
                    this.cancel();
                    this.getList();
                }
                });
            }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cIds = row.cId || this.ids;
      this.$confirm('是否确认删除城市合伙人信息编号为"' + cIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(cIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },

    

    // 积分充值
    addAccumulateFn(){
        console.log(this.ids.join(','),45646)
        addCompanyAccumulate({
            cId: this.ids.join(','),
            accumulatePoint: this.accumulatePoint
        }).then(response => {
            if (response.code === "success") {
                this.msgSuccess("充值成功");
                this.isShowRecharge = false;
                this.getList();
            }
        });
    },
    // 重置积分充值
    resetRecharge(){
        this.accumulatePoint = null;
    }
  }
};
</script>